/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-23
 * Time: 17:50
 */
public class Convert {
    /**
     * @param s       一个给定的字符串
     * @param numRows 给定的行数
     * @return 返回的字符串
     */
    public String convert(String s, int numRows) {
        if (numRows==1){
            return s;
        }
        StringBuilder stringBuffer = new StringBuilder();
        int k = 2 * numRows - 2;
        int n = s.length();

        //1. 处理第一行
        for (int i = 0; i < n; i += k) {
            stringBuffer.append(s.charAt(i));
        }

        // 2. 处理中间⾏
        for (int i = 1; i < numRows - 1; i++) {
            for (int num1 = i, num2 = k - num1; num1 < n || num2 < n; num1 += k, num2 += k) {
                if (num1 < n) stringBuffer.append(s.charAt(num1));
                if (num2 < n) stringBuffer.append(s.charAt(num2));
            }
        }

        // 3. 处理最后⼀⾏
        for(int i = numRows - 1; i < n; i += k)
            stringBuffer.append(s.charAt(i));

        return stringBuffer.toString();
    }
}
